package com.laikan.reader.bookfriend.bean;

import android.databinding.BaseObservable;
import android.databinding.Bindable;
import android.databinding.BindingAdapter;
import android.net.Uri;
import android.text.TextUtils;
import com.facebook.drawee.view.SimpleDraweeView;
import com.laikan.reader.BR;
import com.laikan.reader.R;
import com.laikan.reader.core.LaikanApplication;
import com.socks.library.KLog;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by anrun on 11/21/16.
 */
public class PersonalInfoBean extends BaseObservable implements Serializable {

    private String userAvatorUrl;
    private String userName;
    private String userAccountChannel;
    private String userId;
    private String userGender;
    private String userBirthday;
    private String userIntroduce;

    @Bindable
    public String getUserAvatorUrl() {
        return userAvatorUrl;
    }

    public void setUserAvatorUrl(String userAvatorUrl) {
        this.userAvatorUrl = userAvatorUrl;
        notifyPropertyChanged(BR.userAvatorUrl);
    }

    @Bindable
    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
        notifyPropertyChanged(BR.userName);
    }

    @Bindable
    public String getUserAccountChannel() {
        return userAccountChannel;
    }

    public void setUserAccountChannel(String userAccountChannel) {
        this.userAccountChannel = userAccountChannel;
        notifyPropertyChanged(BR.userAccountChannel);
    }

    @Bindable
    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
        notifyPropertyChanged(BR.userId);
    }

    @Bindable
    public String getUserGender() {
        switch (userGender) {
            case "1":
                return LaikanApplication.getContext().getString(R.string.personal_info_gender_male);
            case "2":
                return LaikanApplication.getContext().getString(R.string.personal_info_gender_female);
            case "3":
                return LaikanApplication.getContext().getString(R.string.personal_info_gender_secret);
            default:
                break;
        }
        return userGender;
    }

    public void setUserGender(String userGender) {
        this.userGender = userGender;
        notifyPropertyChanged(BR.userGender);
    }

    @Bindable
    public String getUserBirthday() {
        return this.userBirthday;
    }

    public void setUserBirthday(String userBirthday) {
        if (TextUtils.isEmpty(userBirthday)) {
            this.userBirthday = "2000-01-01";
        }else if(userBirthday.split("-").length == 1){
            this.userBirthday = getDateToString(Long.parseLong(userBirthday));
        }else {
            this.userBirthday = userBirthday;
        }
        notifyPropertyChanged(BR.userBirthday);
    }

    @Bindable
    public String getUserIntroduce() {
        return userIntroduce;
    }

    public void setUserIntroduce(String userIntroduce) {
        this.userIntroduce = userIntroduce;
        notifyPropertyChanged(BR.userIntroduce);
    }

    /*时间戳转换成字符窜*/
    public String getDateToString(long time) {
        Date d = new Date(time);
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        return sf.format(d);
    }

    @BindingAdapter ({"bind:imageUrl"})
    public static void loadImage(SimpleDraweeView view, String imageUrl) {
        KLog.e("-----------------loadimage");
        view.setImageURI(Uri.parse(imageUrl));
    }
}
